package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.service.IDataDictionaryService;
import cn.wolfcode.crm.query.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("dataDictionary")
public class DataDictionaryController {

    @Autowired
    private IDataDictionaryService dataDictionaryService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "dataDictionary";
    }

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<DataDictionary> query(){
        return dataDictionaryService.selectAll();
    }


    //名称检验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(Long id, String name){
        return dataDictionaryService.selectByName(id,name);
    }

    //编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(DataDictionary dataDictionary){
        JSONResult result = new JSONResult();
        try{
            dataDictionaryService.saveOrUpdate(dataDictionary);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            dataDictionaryService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //等级查询
    @RequestMapping("getAllLevel")
    @ResponseBody
    public List<Map<String,String>> getAllLevel(String level){
        return dataDictionaryService.getAllLevel(level);
    }

}
